home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / c / agl103p.lha / src / agl / mice.c < prev    next >
C/C++ Source or Header  |  1994-12-08  |  7KB  |  300 lines

  1. /******************************************************************************
  2.  
  3. Copyright © 1994 Jason Weber
  4. All Rights Reserved
  5.  
  6. $Id: mice.c,v 1.2.1.3 1994/12/09 05:29:56 jason Exp $
  7.  
  8. $Log: mice.c,v $
  9.  * Revision 1.2.1.3  1994/12/09  05:29:56  jason
  10.  * added copyright
  11.  *
  12.  * Revision 1.2.1.2  1994/11/16  06:25:25  jason
  13.  * check for NOT_EXTERN
  14.  *
  15.  * Revision 1.2.1.1  1994/03/29  05:41:32  jason
  16.  * Added RCS Header
  17.  *
  18.  * Revision 1.2.1.1  2002/03/26  22:04:17  jason
  19.  * Added RCS Header
  20.  *
  21.  * Revision 1.2.1.1  2002/03/26  22:00:51  jason
  22.  * RCS/agl.h,v
  23.  *
  24.  
  25. ******************************************************************************/
  26.  
  27.  
  28. #ifndef NOT_EXTERN
  29. #include"agl.h"
  30. #endif
  31.  
  32. struct MsgPort *GameMP;
  33. struct IOStdReq *GameIO;
  34. struct InputEvent *GameEV;
  35. struct IntuiMessage *GameMS;
  36. BYTE GameEventBuffer[sizeof(struct InputEvent)];
  37.  
  38. struct GamePortTrigger GameTR=
  39.     {
  40.     GPTF_DOWNKEYS|GPTF_UPKEYS,        /* transition trigger */
  41.     300,                            /* seconds * 60Hz (time per auto event) */
  42.     1,1                                /* delta mouse trigger */
  43.     };
  44.  
  45.  
  46. /******************************************************************************
  47. long    start_gameport(void)
  48.  
  49.     returns TRUE if successful
  50.  
  51. ******************************************************************************/
  52. /*PROTOTYPE*/
  53. long start_gameport(void)
  54.     {
  55.     GameEV=(struct InputEvent *)GameEventBuffer;
  56.  
  57.     if((GameMP=CreatePort("RKM_game_port",0)))
  58. /*     if((GameMP=CreatePort(NULL,0))) */
  59.         {
  60.         if(GameIO=(struct IOStdReq *)CreateExtIO(GameMP,sizeof(struct IOStdReq)))
  61. /*         if(GameIO=(struct IOStdReq *)CreateStdIO(GameMP)) */
  62.             {
  63.             if(!OpenDevice("gameport.device",1,(struct IORequest *)GameIO,0))
  64.                 {
  65.                 if(set_controller_type((BYTE)GPCT_MOUSE))
  66.                     {
  67.                     set_trigger_conditions();
  68.  
  69.                     flush_buffer();
  70.  
  71.                     printf("gameport started\n");
  72.                     return TRUE;
  73.                     }
  74.                 else
  75.                     GL_error("can't set game port");
  76.  
  77.                 CloseDevice((struct IORequest *)GameIO);
  78.                 }
  79.             else
  80.                 GL_error("can't open game port");
  81.  
  82.             DeleteExtIO((struct IORequest *)GameIO);
  83.             }
  84.         else
  85.             GL_error("can't create game IO");
  86.  
  87.         DeletePort(GameMP);
  88.         }
  89.     else
  90.         GL_error("can't create game port");
  91.  
  92.     return FALSE;
  93.     }
  94.  
  95.  
  96. /******************************************************************************
  97. void    stop_gameport(void)
  98.  
  99. ******************************************************************************/
  100. /*PROTOTYPE*/
  101. void stop_gameport(void)
  102.     {
  103.     printf("stop gameport\n");
  104.  
  105.     free_gameport();
  106.  
  107.     if(!(CheckIO((struct IORequest *)GameIO)))
  108.         AbortIO((struct IORequest *)GameIO);    /* ask device to abort request, if pending */
  109.  
  110.     WaitIO((struct IORequest *)GameIO);            /* wait for abort, then clean up */
  111.     CloseDevice((struct IORequest *)GameIO);
  112.     DeleteExtIO((struct IORequest *)GameIO);
  113.     DeletePort(GameMP);
  114.  
  115.     printf(" stopped\n");
  116.     }
  117.  
  118.  
  119. /******************************************************************************
  120. void    send_read_request(void)
  121.  
  122. ******************************************************************************/
  123. /*PROTOTYPE*/
  124. void send_read_request(void)
  125.     {
  126.     GameIO->io_Command=GPD_READEVENT;
  127.     GameIO->io_Flags=0;
  128.     GameIO->io_Length=sizeof(struct InputEvent);
  129.     GameIO->io_Data=(APTR)&GameEventBuffer;
  130.     SendIO((struct IORequest *)GameIO);
  131.     }
  132.  
  133.  
  134. /******************************************************************************
  135. void    set_trigger_conditions(void)
  136.  
  137. ******************************************************************************/
  138. /*PROTOTYPE*/
  139. void set_trigger_conditions(void)
  140.     {
  141.     GameIO->io_Command=GPD_SETTRIGGER;
  142.     GameIO->io_Flags=IOF_QUICK;
  143.     GameIO->io_Data=(APTR)&GameTR;
  144.     GameIO->io_Length=sizeof(struct GamePortTrigger);
  145.     DoIO((struct IORequest *)GameIO);
  146.     }
  147.  
  148.  
  149. /******************************************************************************
  150. long    set_controller_type(BYTE type)
  151.  
  152. ******************************************************************************/
  153. /*PROTOTYPE*/
  154. long set_controller_type(BYTE type)
  155.     {
  156.     long success=FALSE;
  157.     BYTE controller_type=0;
  158.  
  159.     Forbid();                                /* start critical section */
  160.  
  161.     GameIO->io_Command=GPD_ASKCTYPE;        /* inquire current status */
  162.     GameIO->io_Length=1;
  163.     GameIO->io_Flags=IOF_QUICK;
  164.     GameIO->io_Data=(APTR)&controller_type;    /* put answer here */
  165.     DoIO((struct IORequest *)GameIO);
  166.  
  167.     if(controller_type==GPCT_NOCONTROLLER)    /* if not in use */
  168.         {
  169.         GameIO->io_Command=GPD_SETCTYPE;
  170.         GameIO->io_Length=1;
  171.         GameIO->io_Flags=IOF_QUICK;
  172.         GameIO->io_Data=(APTR)&type;
  173.         DoIO((struct IORequest *)GameIO);
  174.  
  175.         success=TRUE;
  176.         }
  177.  
  178.     Permit();                                /* end critcal section */
  179.  
  180.     return success;
  181.     }
  182.  
  183.  
  184. /******************************************************************************
  185. void    free_gameport(void)
  186.  
  187. ******************************************************************************/
  188. /*PROTOTYPE*/
  189. void free_gameport(void)
  190.     {
  191.     BYTE type=GPCT_NOCONTROLLER;
  192.  
  193.     GameIO->io_Command=GPD_SETCTYPE;
  194.     GameIO->io_Flags=IOF_QUICK;
  195.     GameIO->io_Length=1;
  196.     GameIO->io_Data=(APTR)&type;
  197.     DoIO((struct IORequest *)GameIO);
  198.     }
  199.  
  200.  
  201. /******************************************************************************
  202. void    flush_buffer(void)
  203.  
  204. ******************************************************************************/
  205. /*PROTOTYPE*/
  206. void flush_buffer(void)
  207.     {
  208.     GameIO->io_Command=CMD_CLEAR;
  209.     GameIO->io_Flags=IOF_QUICK;
  210.     GameIO->io_Data=NULL;
  211.     GameIO->io_Length=0;
  212.     DoIO((struct IORequest *)GameIO);
  213.     }
  214.  
  215.  
  216. /******************************************************************************
  217. long    gameport_event(long *device,short *state,short *dx,short *dy)
  218.  
  219.     only affected values are changed
  220.  
  221.     returns code (FALSE if no message)
  222.  
  223. ******************************************************************************/
  224. /*PROTOTYPE*/
  225. long gameport_event(long *device,short *state,short *dx,short *dy)
  226.     {
  227.     long code;
  228.  
  229.     *device=NULL;
  230.     *state=0;
  231.     *dx=0;
  232.     *dy=0;
  233.  
  234.     GameMS=(struct IntuiMessage *)GetMsg(GameMP);        /* try first without asking */
  235.  
  236.     if(GameMS==NULL)
  237.         {
  238.         send_read_request();                            /* send request */
  239.  
  240.         GameMS=(struct IntuiMessage *)GetMsg(GameMP);    /* try again */
  241.  
  242.         if(GameMS==NULL)                                /* still no message */
  243.             {
  244. /*             printf("gameport_event() no message\n"); */
  245.             return FALSE;                                /* give up for now */
  246.             }
  247.         }
  248.  
  249.     code=GameEV->ie_Code;
  250.     switch(code)
  251.         {
  252.         case IECODE_LBUTTON:
  253.             printf("gameport_event() left down\n");
  254.             *device=BPAD1;
  255.             *state=TRUE;
  256.             break;
  257.         case IECODE_LBUTTON+IECODE_UP_PREFIX:
  258.             printf("gameport_event() left up\n");
  259.             *device=BPAD1;
  260.             *state=FALSE;
  261.             break;
  262.         case IECODE_MBUTTON:
  263.             printf("gameport_event() middle down\n");
  264.             *device=BPAD2;
  265.             *state=TRUE;
  266.             break;
  267.         case IECODE_MBUTTON+IECODE_UP_PREFIX:
  268.             printf("gameport_event() middle up\n");
  269.             *device=BPAD2;
  270.             *state=FALSE;
  271.             break;
  272.         case IECODE_RBUTTON:
  273.             printf("gameport_event() right down\n");
  274.             *device=BPAD3;
  275.             *state=TRUE;
  276.             break;
  277.         case IECODE_RBUTTON+IECODE_UP_PREFIX:
  278.             printf("gameport_event() right up\n");
  279.             *device=BPAD3;
  280.             *state=FALSE;
  281.             break;
  282.         case IECODE_NOBUTTON:
  283. /*             printf("gameport_event() no button\n"); */
  284.             break;
  285.         default:
  286.             printf("gameport_event() default\n");
  287.             return FALSE;
  288.             break;
  289.         }
  290.  
  291.     *dx=  GameEV->ie_X;
  292.     *dy= -GameEV->ie_Y;
  293.  
  294. /*     printf("gameport_event() code=%d %2d,%2d\n",code,GameEV->ie_X,GameEV->ie_Y); */
  295.  
  296. /*     ReplyMsg((struct Message *)GameMS); */
  297.  
  298.     return code;
  299.     }
  300.